﻿using System;
using System.Collections.Generic;
using System.Text;
using DDBuildTools.DataAccess.Components;
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;

namespace DDBuildTools
{
    /// <summary>
    /// 生成html
    /// </summary>
    public class DDBuildHtml:IBuildFile
    {
        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="TableList"></param>
        /// <param name="DataBaseCode"></param>
        /// <param name="TemplateUrl"></param>
        /// <param name="SavePath"></param>
        /// <returns></returns>
        public bool BuildFile(List<TableEntity> TableList, string DataBaseCode, string TemplateUrl, string SavePath)
        {
            bool rBool = false;

            try
            {
                string TemplateDir = AppDomain.CurrentDomain.BaseDirectory + Common.TemplateList[TemplateUrl];

                StringTemplateGroup group = new StringTemplateGroup("MyGroup", new LoadFileHtm(TemplateDir), typeof(DefaultTemplateLexer));
                StringTemplate IndexTemplate = group.GetInstanceOf("index");
                IndexTemplate.SetAttribute("DataBaseName", DataBaseCode);


                StringTemplate IndexLeft = group.GetInstanceOf("left");
                IndexLeft.SetAttribute("DataBaseName", DataBaseCode);
                IndexLeft.SetAttribute("TableEntity", TableList);


                StringTemplate IndexTable = group.GetInstanceOf("Table");
                IndexTable.SetAttribute("TableEntity", TableList);
                IndexTable.SetAttribute("DataBaseName", DataBaseCode);


                Common.CopyFolder(TemplateDir, SavePath);

                Common.WriteFile(IndexTemplate.ToString(), SavePath + @"\index.htm");
                Common.WriteFile(IndexLeft.ToString(), SavePath + @"\left.htm");
                Common.WriteFile(IndexTable.ToString(), SavePath + @"\Table.htm");
                rBool = true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("DDBuildTools Error", ex.ToString());
            }
            return rBool;
        }
    }
}
